<!DOCTYPE html>
<html>
<head>
	<title>用户提交</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title></title>
    <script src="js/mui.js"></script>
    <link href="css/mui.css" rel="stylesheet"/>
	<link href="css/common.css" rel="stylesheet"/>
	<link href="js/layui/css/layui.css" rel="stylesheet"/>
	<!-- Nicky -->
 	<link rel="stylesheet" href="css/nicky.css" /> 
 	<script src="js/jquery.min.js"></script>
 	<script src="js/webdb.js"></script>
</head>
<body class="bg">
	<header class="mui-bar mui-bar-nav mui-bar-transparent bg main-header" >
		<div class="back"><a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left back" >返回</a></div>
		<div class="back" style="text-align: center;"><a class="mui-icon  mui-pull-center back" >添加用户</a></div>
   </header>
	<div class="mui-content bg" >
		<div class="formBox">
			<p>
				<label>用户名</label>
				<input type="text" placeholder="请输入用户名" id="user_account" value="" />
			</p>
			<p>
				<label>密码</label>
				<input type="password" placeholder="请输入密码" id="user_pwd" value="" />
			</p>
			<p>
				<label>姓名</label>
				<input type="text" placeholder="请输入姓名" id="user_name" />
			</p>
			<p>
				<label>角色</label>
				<select id="user_type">
					<option value="">请选择...</option>
					<option value="B">班长</option>
					<option value="Z">质检员</option>
					<option value="F">复检员</option>
				</select>
			</p>
			<p>
				<label>地磅开关</label>
				<span id="mySwitch2" class="mui-switch"><span class="mui-switch-handle"></span></span>
			</p>
		</div> 
		<div class="btnBox btnBox2"> 
			<button class="nbtn" id="btnBack">返 回</button> 
			<button class="nbtn" id="btnSave">保 存</button> 
		</div> 
   </div>
</body>
<script src="js/common.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery.min.js.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var isKT = false;
    mui.init();
    mui.plusReady(function(){
    	var old_back = mui.back;
    	mui.back = function() {
    		var wobj = plus.webview.getWebviewById("yhgl.html");
    		wobj.reload(true);
    		old_back();
    	};
    	select('users',TBS.users,{},function(dat){
    		for (var i=0;i<dat.length;i++) {
    			console.log(JSON.stringify(dat.item(i)));
    		}
    	});
    	
    	$('#btnBack').click(function(){ mui.back(); });
    	$('#btnSave').click(function(){ 
    		var isActive = document.getElementById("mySwitch2").classList.contains("mui-active");
    		var user = {
    			'ID':$("#user_account").val().trim(),
    			'PWD':$("#user_pwd").val().trim(),
    			'UNAME':$("#user_name").val().trim(),
    			'USERS1':$("#user_type").val().trim(),
    			'USERS2':isActive?1:0
    		};
    		if(user.ID.length<1){ mui.toast('请输入用户名'); return false; }
    		if(user.PWD.length<1){ mui.toast('请输入密码'); return false; }
    		if(user.PWD.length>0 && user.PWD.length<5){ mui.toast('密码长度不能少于5位数'); return false; }
    		if(user.USERS1.length<1){ mui.toast('请选择角色'); return false; }
    		select('users',TBS.users,{where:{ID:user.ID}},function(dat){
				if(dat.length){
					mui.toast('用户名已存在'); return false;
				}else{
					var row = [];
					$.each(user, function(i,d) {
						row.push(d);
					});
					insert('users',TBS.users,row,function(row){
						mui.back();
					});
				}
			});
    	});
    });
     
</script>
</html>